﻿/*
 *
 * Action.cs
 * For action -> controller_name.php in CI
 * 
 * Author @semarketir
 * 
 * CICI is simple tool for generate Controller, Model, and View in CodeIgniter ( or CI ) Framework
 * CICI can used to setup configurations in CI ( Database, Autoload, Config ) <- Next Version
 * CICI just can to used for default directory of CodeIgniter ( or CI ) Framework
 * 
 * CICI v.0.0.4 Beta
 * 
 * Support with CI 2.X ( or heigh )
 * 
 * License Under GPL
 * 
 * 
 * Salam, 
 * semar ketir
 * 
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace cici
{
    class Action
    {
        private string path_application;
        private StreamWriter streamWritter;
        private StreamReader streamReader;

        public Action()
        {
            this.path_application = Environment.CurrentDirectory + @"\application\controllers";
        }

        private void createActionToController(string controllerFile, string actionName)
        {

            this.streamReader = new StreamReader(controllerFile);

            string temp_string = this.streamReader.ReadToEnd();
            StringBuilder stringBuilder = new StringBuilder(temp_string);
            int pos = -1;

            if (temp_string.IndexOf("public function") != -1)
            {
                pos = temp_string.IndexOf("public function");
            }
            else if (temp_string.IndexOf("protected function") != -1)
            {
                pos = temp_string.IndexOf("protected function");
            }
            else if (temp_string.IndexOf("private function") != -1)
            {
                pos = temp_string.IndexOf("private function");
            }

            if (pos != -1)
            {
                stringBuilder.Insert(pos, this.templateAction(actionName));

                this.streamReader.Close();

                this.streamWritter = new StreamWriter(controllerFile);
                this.streamWritter.Write(stringBuilder);
                this.streamWritter.Flush();
                this.streamWritter.Close();
            }
        }

        private string[] checkFolderController(string tempClassName)
        {

            string[] temp_folder_controller_split = tempClassName.Split('.');

            int length_array = temp_folder_controller_split.Length - 1;

            string class_name_of_controller = "Noclass";

            int className_of_index_or_array = 0;

            for (int temp_loop = length_array; temp_loop > 0; temp_loop--)
            {
                if (temp_folder_controller_split[temp_loop].Length > 0)
                {
                    class_name_of_controller = temp_folder_controller_split[temp_loop];
                    className_of_index_or_array = temp_loop;
                    break;
                }
            }

            string requr_folder_controller = "";

            for (int temp_loop2 = 0; temp_loop2 < className_of_index_or_array; temp_loop2++)
            {
                if (temp_folder_controller_split[temp_loop2].Length > 0)
                {
                    requr_folder_controller += @"\" + temp_folder_controller_split[temp_loop2];
                }
            }
            requr_folder_controller = this.path_application + requr_folder_controller;

            return new string[] { requr_folder_controller, class_name_of_controller };
        }

        public Boolean createAction(string actionAndAction)
        {
            Boolean status = false;

            string[] temps = actionAndAction.Split(';');

            foreach (string temp in temps)
            {
                string[] temp_abis = temp.Split('=');
                if (temp_abis.Length > 1)
                {
                    string controllerName = temp_abis[0];

                    string actionName = temp_abis[1];

                    Match m = Regex.Match(controllerName, @"[.]");
                    if (m.Success)
                    {
                        string[] temp_folder_and_controller =
                            this.checkFolderController(controllerName);

                        string controllerNameClass = temp_folder_and_controller[1].Substring(0, 1).ToUpper() + temp_folder_and_controller[1].Remove(0, 1);
                        string controllerFile = temp_folder_and_controller[0] + @"\" + temp_folder_and_controller[1] + ".php";

                        if (Directory.Exists(this.path_application))
                        {
                            if (File.Exists(controllerFile))
                            {
                                this.createActionToController(controllerFile, actionName);

                                status = true;
                            }
                        }

                    }
                    else
                    {
                        string controllerNameClass = controllerName.Substring(0, 1).ToUpper() + controllerName.Remove(0, 1);
                        string controllerFile = this.path_application + @"\" + controllerName + ".php";

                        if (Directory.Exists(this.path_application))
                        {
                            if (File.Exists(controllerFile))
                            {
                                this.createActionToController(controllerFile, actionName);
                                status = true;
                            }
                        }
                    }
                }
            }

            return status;
        }

        private string templateAction(string actionName)
        {

            string template = "public function " + actionName + "()\n" +
                "\t{\n" +
                "\t\t//your code in here :)\n" +
                "\t}\n\n\t";

            return template;
        }
    }
}
